package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import java.util.List;
import com.sandking.io.SK_InputStream;
import com.sandking.db.cache.TjzxbbCache;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 统计在线报表
 */
public class Tjzxbb {

	public static final String TABLENAME = "统计在线报表";
	public static final String CLASSNAME = "Tjzxbb"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** 服务器id */
	private int fwqid;
	
	/** 服务器名称 */
	private String fwqmc;
	
	/** 渠道 */
	private String qd;
	
	/** 最大在线 */
	private int zdzx;
	
	/** 平均在线 */
	private int pjzx;
	
	/** 上线人数 */
	private int sxrs;
	
	/** 总在线时长 */
	private long zzxsc;
	
	/** 时间 */
	private java.util.Date sj;
	
	
	public Tjzxbb() {
		super();
	}
	
	public Tjzxbb(int id, int fwqid, String fwqmc, String qd, int zdzx, int pjzx, int sxrs, long zzxsc, java.util.Date sj) {
		super();
		this.id = id;
		this.fwqid = fwqid;
		this.fwqmc = fwqmc;
		this.qd = qd;
		this.zdzx = zdzx;
		this.pjzx = pjzx;
		this.sxrs = sxrs;
		this.zzxsc = zzxsc;
		this.sj = sj;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getFwqid() {
		return fwqid;
	}
	
	public void setFwqid(int fwqid) {
		this.fwqid = fwqid;
		addUpdateColumn("服务器id",fwqid);
	}
	
	public void changeFwqidWith(int fwqid){
		this.fwqid += fwqid;
		setFwqid(this.fwqid);
	}
	
	public void changeFwqidWithMin(int fwqid,int min){
		this.fwqid += fwqid;
		this.fwqid = this.fwqid < min ? min : this.fwqid;
		setFwqid(this.fwqid);
	}
	
	public void changeFwqidWithMax(int fwqid,int max){
		this.fwqid += fwqid;
		this.fwqid = this.fwqid > max ? max : this.fwqid;
		setFwqid(this.fwqid);
	}
	
	public void changeFwqidWithMaxMin(int fwqid,int max,int min){
		this.fwqid += fwqid;
		this.fwqid = this.fwqid < min ? min : this.fwqid;
		this.fwqid = this.fwqid > max ? max : this.fwqid;
		setFwqid(this.fwqid);
	}	
	public String getFwqmc() {
		return fwqmc;
	}
	
	public void setFwqmc(String fwqmc) {
		this.fwqmc = fwqmc;
		addUpdateColumn("服务器名称",fwqmc);
	}
	
	public String getQd() {
		return qd;
	}
	
	public void setQd(String qd) {
		this.qd = qd;
		addUpdateColumn("渠道",qd);
	}
	
	public int getZdzx() {
		return zdzx;
	}
	
	public void setZdzx(int zdzx) {
		this.zdzx = zdzx;
		addUpdateColumn("最大在线",zdzx);
	}
	
	public void changeZdzxWith(int zdzx){
		this.zdzx += zdzx;
		setZdzx(this.zdzx);
	}
	
	public void changeZdzxWithMin(int zdzx,int min){
		this.zdzx += zdzx;
		this.zdzx = this.zdzx < min ? min : this.zdzx;
		setZdzx(this.zdzx);
	}
	
	public void changeZdzxWithMax(int zdzx,int max){
		this.zdzx += zdzx;
		this.zdzx = this.zdzx > max ? max : this.zdzx;
		setZdzx(this.zdzx);
	}
	
	public void changeZdzxWithMaxMin(int zdzx,int max,int min){
		this.zdzx += zdzx;
		this.zdzx = this.zdzx < min ? min : this.zdzx;
		this.zdzx = this.zdzx > max ? max : this.zdzx;
		setZdzx(this.zdzx);
	}	
	public int getPjzx() {
		return pjzx;
	}
	
	public void setPjzx(int pjzx) {
		this.pjzx = pjzx;
		addUpdateColumn("平均在线",pjzx);
	}
	
	public void changePjzxWith(int pjzx){
		this.pjzx += pjzx;
		setPjzx(this.pjzx);
	}
	
	public void changePjzxWithMin(int pjzx,int min){
		this.pjzx += pjzx;
		this.pjzx = this.pjzx < min ? min : this.pjzx;
		setPjzx(this.pjzx);
	}
	
	public void changePjzxWithMax(int pjzx,int max){
		this.pjzx += pjzx;
		this.pjzx = this.pjzx > max ? max : this.pjzx;
		setPjzx(this.pjzx);
	}
	
	public void changePjzxWithMaxMin(int pjzx,int max,int min){
		this.pjzx += pjzx;
		this.pjzx = this.pjzx < min ? min : this.pjzx;
		this.pjzx = this.pjzx > max ? max : this.pjzx;
		setPjzx(this.pjzx);
	}	
	public int getSxrs() {
		return sxrs;
	}
	
	public void setSxrs(int sxrs) {
		this.sxrs = sxrs;
		addUpdateColumn("上线人数",sxrs);
	}
	
	public void changeSxrsWith(int sxrs){
		this.sxrs += sxrs;
		setSxrs(this.sxrs);
	}
	
	public void changeSxrsWithMin(int sxrs,int min){
		this.sxrs += sxrs;
		this.sxrs = this.sxrs < min ? min : this.sxrs;
		setSxrs(this.sxrs);
	}
	
	public void changeSxrsWithMax(int sxrs,int max){
		this.sxrs += sxrs;
		this.sxrs = this.sxrs > max ? max : this.sxrs;
		setSxrs(this.sxrs);
	}
	
	public void changeSxrsWithMaxMin(int sxrs,int max,int min){
		this.sxrs += sxrs;
		this.sxrs = this.sxrs < min ? min : this.sxrs;
		this.sxrs = this.sxrs > max ? max : this.sxrs;
		setSxrs(this.sxrs);
	}	
	public long getZzxsc() {
		return zzxsc;
	}
	
	public void setZzxsc(long zzxsc) {
		this.zzxsc = zzxsc;
		addUpdateColumn("总在线时长",zzxsc);
	}
	
	public void changeZzxscWith(long zzxsc){
		this.zzxsc += zzxsc;
		setZzxsc(this.zzxsc);
	}
	
	public void changeZzxscWithMin(long zzxsc,long min){
		this.zzxsc += zzxsc;
		this.zzxsc = this.zzxsc < min ? min : this.zzxsc;
		setZzxsc(this.zzxsc);
	}
	
	public void changeZzxscWithMax(long zzxsc,long max){
		this.zzxsc += zzxsc;
		this.zzxsc = this.zzxsc > max ? max : this.zzxsc;
		setZzxsc(this.zzxsc);
	}
	
	public void changeZzxscWithMaxMin(long zzxsc,long max,long min){
		this.zzxsc += zzxsc;
		this.zzxsc = this.zzxsc < min ? min : this.zzxsc;
		this.zzxsc = this.zzxsc > max ? max : this.zzxsc;
		setZzxsc(this.zzxsc);
	}	
	public java.util.Date getSj() {
		return sj;
	}
	
	public void setSj(java.util.Date sj) {
		this.sj = sj;
		addUpdateColumn("时间",sj);
	}
	
	
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("fwqid", this.fwqid);
        result.put("fwqmc", this.fwqmc);
        result.put("qd", this.qd);
        result.put("zdzx", this.zdzx);
        result.put("pjzx", this.pjzx);
        result.put("sxrs", this.sxrs);
        result.put("zzxsc", this.zzxsc);
        result.put("sj", this.sj);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("服务器id", this.fwqid);
        result.put("服务器名称", this.fwqmc);
        result.put("渠道", this.qd);
        result.put("最大在线", this.zdzx);
        result.put("平均在线", this.pjzx);
        result.put("上线人数", this.sxrs);
        result.put("总在线时长", this.zzxsc);
        result.put("时间", this.sj);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.fwqid);
		    SK_OutputStream.writeString(out,this.fwqmc);
		    SK_OutputStream.writeString(out,this.qd);
		    SK_OutputStream.writeInt(out,this.zdzx);
		    SK_OutputStream.writeInt(out,this.pjzx);
		    SK_OutputStream.writeInt(out,this.sxrs);
		    SK_OutputStream.writeLong(out,this.zzxsc);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Tjzxbb createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Tjzxbb tjzxbb = new Tjzxbb();
		    tjzxbb.id = SK_InputStream.readInt(in,null);
		    tjzxbb.fwqid = SK_InputStream.readInt(in,null);
		    tjzxbb.fwqmc = SK_InputStream.readString(in,null);
		    tjzxbb.qd = SK_InputStream.readString(in,null);
		    tjzxbb.zdzx = SK_InputStream.readInt(in,null);
		    tjzxbb.pjzx = SK_InputStream.readInt(in,null);
		    tjzxbb.sxrs = SK_InputStream.readInt(in,null);
		    tjzxbb.zzxsc = SK_InputStream.readLong(in,null);
		    return tjzxbb;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Tjzxbb> createForColumnNameList(List<Map<String, Object>> list){
    	List<Tjzxbb> tjzxbbs = new ArrayList<Tjzxbb>();
		for (Map<String, Object> map : list) {
			tjzxbbs.add(createForColumnNameMap(map));
		}
		return tjzxbbs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Tjzxbb createForColumnNameMap(Map<String, Object> map){
    	Tjzxbb obj = new Tjzxbb();
	    obj.id = SK_Map.getInt("id", map);
	    obj.fwqid = SK_Map.getInt("服务器id", map);
	    obj.fwqmc = SK_Map.getString("服务器名称", map);
	    obj.qd = SK_Map.getString("渠道", map);
	    obj.zdzx = SK_Map.getInt("最大在线", map);
	    obj.pjzx = SK_Map.getInt("平均在线", map);
	    obj.sxrs = SK_Map.getInt("上线人数", map);
	    obj.zzxsc = SK_Map.getLong("总在线时长", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Tjzxbb> createForList(List<Map<String, Object>> list){
    	List<Tjzxbb> tjzxbbs = new ArrayList<Tjzxbb>();
		for (Map<String, Object> map : list) {
			tjzxbbs.add(createForColumnNameMap(map));
		}
		return tjzxbbs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Tjzxbb createForMap(Map<String, Object> map){
    	Tjzxbb obj = new Tjzxbb();
	    obj.id = SK_Map.getInt("id", map);
	    obj.fwqid = SK_Map.getInt("fwqid", map);
	    obj.fwqmc = SK_Map.getString("fwqmc", map);
	    obj.qd = SK_Map.getString("qd", map);
	    obj.zdzx = SK_Map.getInt("zdzx", map);
	    obj.pjzx = SK_Map.getInt("pjzx", map);
	    obj.sxrs = SK_Map.getInt("sxrs", map);
	    obj.zzxsc = SK_Map.getLong("zzxsc", map);
        return obj;
    }
    
    public static Tjzxbb createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Tjzxbb> createForJson(List<String> jsons){
    	List<Tjzxbb> tjzxbbs = new ArrayList<Tjzxbb>();
    	for(String json : jsons){
    		tjzxbbs.add(createForJson(json));
    	}
    	return tjzxbbs;
    }
    
    /** 延迟插入数据库 */
    public Tjzxbb insert(){
    	return TjzxbbCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Tjzxbb update(){
    	return TjzxbbCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return TjzxbbCache.delete(this);
    }
    /** 即时插入数据库 */
    public Tjzxbb insertAndFlush(){
    	return TjzxbbCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Tjzxbb updateAndFlush(){
    	return TjzxbbCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return TjzxbbCache.deleteAndFlush(this);
    }
}